<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="//unpkg.com/layui@2.6.8/dist/css/layui.css">

    <!-- 引入 layui.js -->
    <!--<script th:src="@{plugins/layer/layer.js}"></script>-->
    <!--<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>-->
    　<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="//unpkg.com/layui@2.6.8/dist/layui.js"></script>

</head>
<body>

<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form" >
        <div class="layui-form-item">
            <label for="L_uname" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_uname" name="uname" required="" th:value="${user.uname}" lay-verify="uname"
                       autocomplete="off" class="layui-input">
                <input type="hidden" id="uid" name="uid" th:value="${user.uid}">
                <input type="hidden" id="L_upass" name="upass" th:value="${user.upass}" required="" lay-verify="upass"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>将会成为您唯一的登入名
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_phone" class="layui-form-label">
                <span class="x-red">*</span>电话
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_phone" name="phone" required="" th:value="${user.phone}" lay-verify="phone"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="x-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_email" name="email" required="" th:value="${user.email}" lay-verify="email"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <select name="gender" lay-verify="required">
                    <option th:value="${user.gender}" th:text="${user.gender}"></option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" id="userheader">
                    <i class="layui-icon">&#xe67c;</i>头像上传
                </button>
                <img id="header" width="90" height="90" th:src="@{'../'+${user.imgurl}}">
                <input type="hidden" id="imgurl" name="imgurl" th:value="${user.imgurl}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label  class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="update" lay-submit="">
                更新
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form','layer','upload'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,upload = layui.upload
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            uname: function(value){
                if(value.length <3){
                    return '用户名至少得3个字符啊';
                }
            }
            ,phone:function(value){
                if (value.length<11||value.length>11){
                    return '电话号码格式错误'
                }
            }
        });

        //监听提交
        form.on('submit(update)', function(data){
            console.log(data);
            //发异步，把数据提交给后端
            $.ajax({
                url:"/user/updateuser",
                data:data.field,
                dataType:"json",
                success:function (sdata) {
                    if(sdata.code==0){
                        layer.alert("修改成功", {icon: 1,time:2000},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);

                        });
                    }else{
                        layer.alert(sdata.msg,
                            {icon: 1,time:2000})
                    }

                }
            });


            return false;
        });
        //头像上传
        //执行实例
        var uploadInst = upload.render({
            elem: '#userheader' //绑定元素
            ,url: '../upload'
            ,done: function(res){
                //上传完毕回调
                layer.msg(res.msg,{
                    icon:1,
                    time:2000
                },function(){
                    $("#header").attr("src","../downheader?fileName="+res.data.filename);
                    $("#imgurl").val("downheader?fileName="+res.data.filename);
                })
            }
            ,error: function(){
                //请求异常回调
            }
        });


    });
</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>
</body>
</html>